//
//  DDSoundViewController.m
//  ialone
//
//  Created by lanou on 16/1/12.
//  Copyright © 2016年 JasperSong. All rights reserved.
//

#import "DDSoundViewController.h"
#import "DDSoundTableViewCell.h"
#import "DDSoundDetailsViewController.h"
#import "DDSoundModel.h"
#import "PKRequestManager.h"

@interface DDSoundViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *dataArray;

@property (nonatomic,assign)NSInteger requestStart;

@property (nonatomic,strong)NSString *pageid;

@property (nonatomic,strong)NSString *create_time;
@end

@implementation DDSoundViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
        
    // Do any additional setup after loading the view.
    _dataArray = [NSMutableArray array];
    _requestStart = 1;
    _pageid = @"0";
    _create_time = @"0";
    self.titleLabel.text = _str1;
    self.titleLabel.font = [UIFont systemFontOfSize:28];
    
    [self.leftButton addTarget:self action:@selector(leftButton:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.leftButton setImage:[UIImage imageNamed:@"item_cancel@2x"] forState:(UIControlStateNormal)];
    
    [self addTableView];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        // 进入刷新状态后会自动调用这个block
        _requestStart = 1;
        _create_time = @"0";
        _pageid = @"0";
        [self setUpData];
    }];
    
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        // 进入刷新状态后会自动调用这个block
        _requestStart += 1 ;
        DDSoundModel *model = _dataArray[_dataArray.count - 1] ;
        _create_time = model.create_time;
               _pageid = model.pageid;
        [self setUpData];
        // 更改start
        
        
        
    }];
    [self hubShow];
    [self setUpData];
    
}
- (void)leftButton:(UIButton *)button
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)setUpData
{
    //http://static.owspace.com/index.php?m=Home&c=Api&a=getList&model=3&p=1&client=iphone&page_id=0&create_time=0&device_id=B709CBC5-0E2F-4C89-9E24-6AFBBA86C58C&iOS_version=1.0.0
    
//    http://static.owspace.com/index.php?m=Home&c=Api&a=getList&model=3&p=2&client=iphone&page_id=291460&create_time=1449126000&device_id=B709CBC5-0E2F-4C89-9E24-6AFBBA86C58C&iOS_version=1.0.0
//    http://static.owspace.com/index.php?m=Home&c=Api&a=getList&model=1&p=2&client=iphone&page_id=291524&create_time=1438398300&device_id=B709CBC5-0E2F-4C89-9E24-6AFBBA86C58C&iOS_version=1.0.0
    NSString *str = [_str stringByReplacingOccurrencesOfString:@"p=1" withString:[NSString stringWithFormat:@"p=%ld",_requestStart]];
    str = [str stringByReplacingOccurrencesOfString:@"page_id=0" withString:[NSString stringWithFormat:@"page_id=%@",_pageid]];
    str = [str stringByReplacingOccurrencesOfString:@"create_time=0" withString:[NSString stringWithFormat:@"create_time=%@",_create_time]];
    
    [PKRequestManager requestWithUrl:str parmDic:nil method:GET succeedWithData:^(NSData *data) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    
        [self hubHide];
        
        NSArray *array = dic[@"datas"];
        if (_requestStart == 1) {
            [_dataArray removeAllObjects];
        }
        for (NSDictionary *oneDic in array) {
            DDSoundModel *model = [[DDSoundModel alloc]init];
            
            [model setValuesForKeysWithDictionary:oneDic];
            
            [self.dataArray addObject:model];
        }
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        [self.tableView reloadData];
        //NSLog(@"%@",self.dataArray);
        
        
        
        
    } failedWithError:^(NSError *connectionError) {
        
    }];
}

- (void)addTableView
{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, kScreenWidth, kScreenHeight - 64) style:(UITableViewStylePlain)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 120;
    
    [self.view addSubview:self.tableView];
    
    [self.tableView registerClass:[DDSoundTableViewCell class] forCellReuseIdentifier:@"DDSoundTableViewCell"];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DDSoundTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DDSoundTableViewCell" forIndexPath:indexPath];
    
    DDSoundModel *model = self.dataArray[indexPath.row];
    
    cell.model = model;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DDSoundModel *model = self.dataArray[indexPath.row];
    DDSoundDetailsViewController *sdVC = [[DDSoundDetailsViewController alloc]init];
    sdVC.html5 = model.html5;
    sdVC.pageid = model.pageid;
    sdVC.category = model.category;
    sdVC.titlename = model.title;
    sdVC.thumbnail = model.thumbnail;
    sdVC.author = model.author;
    [self.navigationController pushViewController:sdVC animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
